package com.ez.supervise.action.biz;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.ez.supervise.action.base.BaseJsonAction;
import com.ez.supervise.core.Page;
import com.ez.supervise.core.Result;
import com.ez.supervise.model.biz.DemocracyGoal;
import com.ez.supervise.service.DemocracyGoalService;
import com.ez.supervise.util.JsonUtils;

public class DemocracyGoalSettingAction extends BaseJsonAction {

    private static final long serialVersionUID = 6639654058416236294L;
    private Long id;
    private DemocracyGoalService democracyGoalService;
    private DemocracyGoal democracyGoal;
    private Page page;
    private String goalIds;
    private String orgType;

    public String saveSetting() {
	try {
	    democracyGoalService.save(orgType, goalIds);
	    Result result = new Result();
	    outJsonString(JSONObject.fromObject(result).toString());
	} catch (Exception e) {
	    e.printStackTrace();
	    outJsonString(RESULT_FAIL);
	}

	return SUCCESS;
    }

    public void setGoalIds(String goalIds) {
        this.goalIds = goalIds;
    }

    public void setOrgType(String orgType) {
        this.orgType = orgType;
    }

    public String findSettingByPage() {
	try {
	    page = new Page();
	    HttpServletRequest request = getRequest();
	    page.putCondition("year", NumberUtils.isNumber(request.getParameter("year")) ? Long.valueOf(request
		.getParameter("year")) : null);
	    page.putCondition("orgType", request.getParameter("orgType"));
	    int start = NumberUtils.toInt(request.getParameter("start"), 0);
	    int limit = NumberUtils.toInt(request.getParameter("limit"), 20);
	    page.setStart(start++);
	    page.setLimit(limit);
	    page.addPageInfo();
	    democracyGoalService.findSettingByPage(page);
	    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
		"isDeleted", "new" }));

	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    /**
     * @return
     */
    public String deleteSetting() {
	try {
	    if (id != null && id > 0) {
		democracyGoalService.deleteSetting(id);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    /**
     * @return
     * @throws Exception
     */

    public Long getId() {
	return id;
    }

    public void setId(Long id) {
	this.id = id;
    }

    public void setDemocracyGoalService(DemocracyGoalService democracyGoalService) {
	this.democracyGoalService = democracyGoalService;
    }

    public DemocracyGoal getDemocracyGoal() {
	return democracyGoal;
    }

    public void setDemocracyGoal(DemocracyGoal democracyGoal) {
	this.democracyGoal = democracyGoal;
    }

    public Page getPage() {
	return page;
    }

    public void setPage(Page page) {
	this.page = page;
    }

}
